@echo off
echo ===== 第二课堂管理系统Mapper和Domain问题修复脚本 =====
echo 此脚本将修复ActivityMapper、CreditRecordMapper和Teacher域对象问题

REM 设置目录变量
set ADMIN_MODULE=.\scms-admin
set SECONDCLASS_MODULE=.\scms-secondclass
set ADMIN_MAPPER_DIR=%ADMIN_MODULE%\src\main\java\com\scms\secondclass\mapper
set SECONDCLASS_MAPPER_DIR=%SECONDCLASS_MODULE%\src\main\java\com\scms\secondclass\mapper
set ADMIN_DOMAIN_DIR=%ADMIN_MODULE%\src\main\java\com\scms\secondclass\domain
set SECONDCLASS_DOMAIN_DIR=%SECONDCLASS_MODULE%\src\main\java\com\scms\secondclass\domain
set ADMIN_MAPPER_XML_DIR=%ADMIN_MODULE%\src\main\resources\mapper\secondclass
set SECONDCLASS_MAPPER_XML_DIR=%SECONDCLASS_MODULE%\src\main\resources\mapper\secondclass

echo 步骤1: 复制ActivityMapper.java...
if exist "%SECONDCLASS_MAPPER_DIR%\ActivityMapper.java" (
    if not exist "%ADMIN_MAPPER_DIR%" (
        mkdir "%ADMIN_MAPPER_DIR%"
        echo 创建目录: %ADMIN_MAPPER_DIR%
    )
    copy "%SECONDCLASS_MAPPER_DIR%\ActivityMapper.java" "%ADMIN_MAPPER_DIR%\"
    if %ERRORLEVEL% equ 0 (
        echo ActivityMapper.java 复制成功！
    ) else (
        echo 错误: 复制 ActivityMapper.java 失败！
        goto :error
    )
) else (
    echo 错误: 源文件 %SECONDCLASS_MAPPER_DIR%\ActivityMapper.java 不存在！
    goto :error
)

echo 步骤2: 复制CreditRecordMapper.xml...
if exist "%ADMIN_MAPPER_XML_DIR%\CreditRecordMapper.xml" (
    if not exist "%SECONDCLASS_MAPPER_XML_DIR%" (
        mkdir "%SECONDCLASS_MAPPER_XML_DIR%"
        echo 创建目录: %SECONDCLASS_MAPPER_XML_DIR%
    )
    copy "%ADMIN_MAPPER_XML_DIR%\CreditRecordMapper.xml" "%SECONDCLASS_MAPPER_XML_DIR%\"
    if %ERRORLEVEL% equ 0 (
        echo CreditRecordMapper.xml 复制成功！
    ) else (
        echo 错误: 复制 CreditRecordMapper.xml 失败！
        goto :error
    )
) else (
    echo 错误: 源文件 %ADMIN_MAPPER_XML_DIR%\CreditRecordMapper.xml 不存在！
    goto :error
)

echo 步骤3: 复制Teacher.java...
if exist "%SECONDCLASS_DOMAIN_DIR%\Teacher.java" (
    if not exist "%ADMIN_DOMAIN_DIR%" (
        mkdir "%ADMIN_DOMAIN_DIR%"
        echo 创建目录: %ADMIN_DOMAIN_DIR%
    )
    copy "%SECONDCLASS_DOMAIN_DIR%\Teacher.java" "%ADMIN_DOMAIN_DIR%\"
    if %ERRORLEVEL% equ 0 (
        echo Teacher.java 复制成功！
    ) else (
        echo 错误: 复制 Teacher.java 失败！
        goto :error
    )
) else (
    echo 错误: 源文件 %SECONDCLASS_DOMAIN_DIR%\Teacher.java 不存在！
    goto :error
)

echo 步骤4: 检查并停止已运行的实例...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr :8089') do (
    echo 发现端口8089被进程PID: %%a占用，尝试终止...
    taskkill /F /PID %%a
    if %ERRORLEVEL% equ 0 (
        echo 成功终止进程PID: %%a
    ) else (
        echo 无法终止进程PID: %%a，请手动终止后重试
    )
)

echo 步骤5: 清理和编译项目...
cd %ADMIN_MODULE%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: 编译失败！
    cd ..
    goto :error
) else (
    echo 编译完成！
)
cd ..

echo 步骤6: 删除重复的Mapper XML文件...
if exist "%ADMIN_MAPPER_XML_DIR%\ActivityMapper.xml" (
    if exist "%SECONDCLASS_MAPPER_XML_DIR%\ActivityMapper.xml" (
        del "%SECONDCLASS_MAPPER_XML_DIR%\ActivityMapper.xml"
        echo 删除重复的ActivityMapper.xml文件。
    )
) else (
    echo 目录中不存在重复的ActivityMapper.xml文件，无需删除。
)

echo 步骤7: 启动系统...
echo 系统正在启动，请稍候...
cd %ADMIN_MODULE%
start java -jar target\scms-admin.jar
cd ..

echo ===== Mapper和Domain问题修复完成！=====
echo 系统已启动，请等待几秒钟后访问 http://localhost:8089
goto :end

:error
echo 修复过程中出现错误，请查看上面的错误信息。

:end 